package com.esotericsoftware.yamlbeans.extend;

import com.esotericsoftware.yamlbeans.YamlConfig;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;

import java.io.*;
import java.util.*;

public class Test {

    @ColumnComment(value = "姓名",sort = 1)
    @Column(value = "kkkk")
    private String name;

    @ColumnComment(value = "年龄",sort = 1)
    private String age;

    @ColumnComment(value = {"Map","多行注释"},sort = 1)
    private Map map = new HashMap();

    @ColumnComment(value = {"测试"})
    private EEEE zzz;

    @Column("items")
    private List<String> list = Arrays.asList("123","321");

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public EEEE getZzz() {
        return zzz;
    }

    public void setZzz(EEEE zzz) {
        this.zzz = zzz;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    enum EEEE implements IEnum{

        AAAA("AAAA","A的意思....."),
        BBBB("BBBB","B的意思....."),
        ;

        EEEE(String code, String name) {
            this.code = code;
            this.name = name;
        }

        private String code;

        private String name;

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }
    }




    public static void main(String[] args) {
//        Map map = new HashMap();
//        map.put("A",EEEE.AAAA);
//        map.put("B",Arrays.asList("1",2,3));
//        Map m = new HashMap();
//        m.put("C1","C1111");
//        m.put("C2","C2222");
//        map.put("C",m);
//
//        Test.SuperClass test = new Test.SuperClass();
//        test.setId("1");
//        test.setName("小明");
//        test.setMap(map);
//
//        Test t = new Test();
//        t.setName("小刚");
//        t.setMap(m);
//        test.setTest(t);
//        save(test);

        Test t = new Test();
        t.setName("小刚");
        t.setAge("20");

        save(t);

        Object red = red();
        System.out.println(red);

    }

    public static <T> void save(T t) {
        String path = "C:\\Users\\wfkh0\\Desktop\\test.yml";
        YamlWriter writer = null;
        try {
            if(t != null){
                writer = new YamlWriter(new FileWriter(path));
                //该行代码是指写文件时不写入全限定类名作为索引
                writer.getConfig().writeConfig.setWriteRootTags(false);
                //所有类中的引用类型都不会输出全限定类名
                writer.getConfig().writeConfig.setWriteRootElementTags(false);
                //设置缩进格数
                writer.getConfig().writeConfig.setIndentSize(2);
                //写入具有默认值的对象字段
                writer.getConfig().writeConfig.setWriteDefaultValues(true);
                //设置为不抛锚点
                writer.getConfig().writeConfig.setAutoAnchor(false);
                //设置为不转义
                writer.getConfig().writeConfig.setEscapeUnicode(false);
                writer.getConfig().writeConfig.setQuoteChar(YamlConfig.Quote.DOUBLE);
                writer.getConfig().setPrivateFields(true);
                writer.write(t);
            }else{
                //即使是空也要创建文件
                File file = new File(path);
                if(!file.exists()){
                    file.createNewFile();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (YamlException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Object red() {
        String path = "C:\\Users\\wfkh0\\Desktop\\test.yml";
        YamlReader reader = null;
        try {
            reader = new YamlReader(new FileReader(path));
            return reader.read();
        } catch (Throwable e) {
        }
        return null;
    }

    static class SuperClass extends Test{
        private String id;

        @ColumnComment(value = "子类",sort = 1)
        @Column(value = "xxxxx")
        private Test test;

        @ColumnComment(value = "开关",sort = 2)
        private Boolean i;

        public Test getTest() {
            return test;
        }

        public void setTest(Test test) {
            this.test = test;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

}
